//
//  CYVideoDetail_TitleTableViewCell.m
//  cyydt
//
//  Created by chu on 2019/1/23.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYVideoDetail_TitleTableViewCell.h"
#import "CYProjectDetail_PhonePop1View.h"

@implementation CYVideoDetail_TitleTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}
- (IBAction)zhankaiAction:(UIButton *)sender {
    if (self.block) {
        self.block();
    }
    
    if ([self.model.isOpen boolValue]) {
        [UIView animateWithDuration:0.1 animations:^{
            sender.imageView.transform = CGAffineTransformMakeRotation(M_PI);
        }];
    }else{
        [UIView animateWithDuration:1 animations:^{
            sender.imageView.transform = CGAffineTransformIdentity;
        }];
    }
}
- (IBAction)zixunAction:(UIButton *)sender {
    
    
    //免费电话
    NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"CYProjectDetail_PhonePop1View" owner:self options:nil];
    CYProjectDetail_PhonePop1View *backView = [nibView objectAtIndex:0];
    backView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
    backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    __weak typeof(self)weakself = self;
    backView.block = ^(NSString * _Nonnull phone) {
        [weakself DianHuaWithPhone:phone];
    };
    [backView show];
}

- (void)DianHuaWithPhone:(NSString *)phone{
    
    //    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [EasyShowLodingView showLoding];
    NSDictionary *param = @{@"itemid":self.model.itemid, @"callee":phone, @"referrer":@"app", @"gform":@14};
    //将图片上传到服务器
    NSString *urlString = [Api_HOST stringByAppendingString:@"call-phone/create"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
 
    //接收类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         nil];
    [manager POST:urlString parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //打印下上传进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [EasyShowLodingView hidenLoding];
        
        NSLog(@"responseObject--- %@",responseObject);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *responseResult = (NSDictionary *)responseObject;
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[@"message"] duration:1.5 position:CSToastPositionCenter];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error -- %@",error);
        [EasyShowLodingView hidenLoding];
        
    }];
    //    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param contentType:@"multipart/form-data" ResponseObject:^(NSDictionary *responseResult) {
    //        [EasyShowLodingView hidenLoding];
    //        NSLog(@"留电话 ---- %@",responseResult);
    //        if ([responseResult objectForKey:@"resError"]) {
    //            [self.view makeToast:ResErrorMessage];
    //        }else{
    //            if ([responseResult[Response_code] integerValue] == Response_value) {
    //                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
    //            }else{
    //                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
    //
    //            }
    //        }
    //    }];
}

- (void)setModel:(CYProjectModel *)model{
    _model = model;
    self.titleLabel.text = model.slogan;
    if ([model.isOpen boolValue]) {
        self.introLabel.text = model.content;
        self.introMarginTopConstraint.constant = 20;
    }else{
        self.introLabel.text = @"";
        self.introMarginTopConstraint.constant = 0;
    }
    
    self.countLabel.text = [NSString stringWithFormat:@"播放量：%@",model.views];
    
    if ([model.itemid integerValue] == 0) {
        self.zixunBtn.hidden = YES;
        self.zixunBtn.userInteractionEnabled = NO;
    }else{
        self.zixunBtn.hidden = NO;
        self.zixunBtn.userInteractionEnabled = YES;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
